import React, { useState } from 'react';
import { Route, Redirect } from 'react-router-dom';
import {message} from "antd";
import Login from '../page/Login';
import BasicLayout from '../layout/BasicLayout';

const PrivateRoute = (props) => {

  const token = window.localStorage.getItem("token")
  const isLogin = token ? true : false;
  const [isLoggedIn, setIsLoggedIn] = useState(isLogin);

  const handleLogin = () => {
    const token = window.localStorage.getItem("token");
    if(token){
      setIsLoggedIn(true);
    }else{
      setIsLoggedIn(false)
    }
  };

  return <>
    {
      isLoggedIn ? <>
        <BasicLayout isLogin={isLoggedIn} handleLogin={handleLogin}></BasicLayout>
      </> : 
      <>
        <Login handleLogin={handleLogin}></Login>
      </>

    }
  </>  
};

export default PrivateRoute;
